--[[
This software is licensed under the zlib license.

  (C) Copyright 2015 Pedro Gimeno Fortea. All rights reserved.

  This software is provided 'as-is', without any express or implied
  warranty. In no event will the author or authors be held liable for any
  damages arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.
]]

strbit = require "strbit"

local good
local bad
local seed

local function to_bits(s)
  local out = ""
  for i = 1, #s do
    local byte = s:byte(i)
    local this = ""
    for bit = 0, 7 do
      this = (byte % 2) .. this
      byte = (byte - byte % 2) / 2
    end
    out = out .. this
  end
  return out
end

local function bshiftleft(s, i)
  len = #s
  if i < 0 then
    i = len+i
    if i < 0 then i = 0 end
    s = string.rep("0", len) .. s
    return s:sub(i+1, i+len)
  end
  s = s .. string.rep("0", len)
  if i > len then i = len end
  return s:sub(i+1, i+len)
end

local function band(s1, s2)
  local s = ""
  if #s1 > #s2 then
    s1 = s1:sub(1, #s2)
  end
  for i = 1, #s1 do
    s = s .. ((s1:byte(i)-48) * (s2:byte(i)-48))
  end
  return s
end

local function bor(s1, s2)
  local s = ""
  if #s1 > #s2 then
    s1 = s1:sub(1, #s2)
  end
  for i = 1, #s1 do
    s = s .. (s1:byte(i)-48) + (s2:byte(i)-48) - (s1:byte(i)-48)*(s2:byte(i)-48)
  end
  return s
end

local function bxor(s1, s2)
  local s = ""
  if #s1 > #s2 then
    s1 = s1:sub(1, #s2)
  end
  for i = 1, #s1 do
    s = s .. (s1:byte(i) + s2:byte(i)) % 2
  end
  return s
end

local function bnot(s1)
  local s = ""
  for i = 1, #s1 do
    s = s .. string.char(97-s1:byte(i))
  end
  return s
end

local function test_shift(s, i)
  local with_lua = bshiftleft(to_bits(s), i)
  local with_strbit = to_bits(strbit.bshiftleft(s, i))
  if with_lua == with_strbit then
    good = good + 1
  else
    bad = bad + 1
    print("Difference: shift of "..i)
    print("Original    = "..to_bits(s))
    print("with_lua    = "..with_lua)
    print("with_strbit = "..with_strbit)
  end
end

local function test_binop(s1, s2, luafn, libfn, name)
  local with_lua = luafn(to_bits(s1), to_bits(s2))
  local with_strbit = to_bits(libfn(s1, s2))
  if with_lua == with_strbit then
    good = good + 1
  else
    bad = bad + 1
    print("Difference in "..name)
    print("Original 1  = "..to_bits(s1))
    print("Original 2  = "..to_bits(s2))
    print("with_lua    = "..with_lua)
    print("with_strbit = "..with_strbit)
  end
end

local function test_not(s)
  local with_lua = bnot(to_bits(s))
  local with_strbit = to_bits(strbit.bnot(s))
  if with_lua == with_strbit then
    good = good + 1
  else
    bad = bad + 1
    print("Difference: not ")
    print("Original    = "..to_bits(s))
    print("with_lua    = "..with_lua)
    print("with_strbit = "..with_strbit)
  end
end


good = 0
bad = 0
test_shift("", -30)
test_shift("", -8)
test_shift("", -3)
test_shift("", 0)
test_shift("", 3)
test_shift("", 8)
test_shift("", 30)

seed = os.time()
print("Seed = " .. seed)
math.randomseed(seed)

local olds
local s = ""

for test = 1, 10000 do
  len = math.random(0,8)
  olds = s
  s = ""
  for i = 1, len do
    s = s .. string.char(math.random(0, 255))
  end
  test_shift(s, math.random(-80, 80))
  test_binop(olds, s, band, strbit.band, "AND")
  test_binop(olds, s, bor, strbit.bor, "OR")
  test_binop(olds, s, bxor, strbit.bxor, "XOR")
  test_not(s)
end

print("Good: " .. good .. "; bad: " .. bad)
